import api from '@/api/message'
import { Message } from 'element-ui';
import moment from 'moment';

const message = {
    state: {
        mesData: [],
        mesPagination: {
            total:  null, //总共有几个
            pageNum: 1, //当前页数
            pageSize: 9, //一个展示几个
        },
        reload: false,
        evaluateData: [
        ],
        evaPagination: {
            total:  null, //总共有几个
            pageNum: 1, //当前页数
            pageSize: 9, //一个展示几个
        },
        personalReload: true,
        personalData: [],
        collectData: []
    },

    mutations: {
        //先设置为false 再设置为true 手动更新分页数据 不可同时设置
        resetPagination: (state) => {
            state.mesPagination = Object.assign({}, {
                total:  null, //总共有几个
                pageNum: 1, //当前页数
                pageSize: 9, //一个展示几个
            });
        },
        reloadPagination: (state, bool) => {
            state.reload = bool;
        },
        reloadPersonPagination: (state, bool) => {
            state.personalReload = bool;
        },
        saveMessage:(state, { list, mesPagination }) => {
            state.mesData.splice(0 ,state.mesData.length);
            state.mesPagination = Object.assign({}, mesPagination);
            state.reload = true;
            //未读的放前面 1是已读 2是未读
            list.sort((a,b) => {
                return a.message_status < b.message_status ? 1 : -1;
            });
            let getTypeName = (type) => {
                switch(type){
                    case 1:
                        return '天鹿物流';
                    case 2:
                        return '系统消息';
                    case 3: 
                        return '活动消息';
                    case 4:
                        return '店铺消息';
                }
            };
            list.forEach(item => {
                item.key = item.message_id;
                item.message_createTime = moment(item.message_createTime).format('YYYY-MM-DD HH:mm')
                item.isSelect = false;
                item.isRead = item.message_status === 1;
                item.message_type = getTypeName(item.message_type);
            });
            state.mesData = [...list];
        },
        saveEvaluate: (state, { list, evaPagination }) => {
            state.evaluateData.splice(0 ,state.evaluateData.length);
            state.evaPagination = Object.assign(evaPagination);

            list.forEach((item,index) => {
                item.key = index;
                item.evaluation_time = moment(item.evaluation_time).format('YYYY-MM-DD HH:mm');
            });
            state.evaluateData = [...list];
        },
        handleSelectAll (state,check) {
            state.mesData.forEach(item => {
              item.isSelect = check;
            });
        },
        handleSelect (state, {check, key}) {
            state.mesData.forEach(item => {
              if (item.key === key) {
                item.isSelect = check;
              }
            });
        },
        readChange (state,selectArr) {
            state.mesData.forEach(item => {
                if(selectArr.includes(item.message_id)){
                    item.isRead = true;
                    item.isSelect = false;
                }
            })
        },
        deleteChange (state) {
            let arr = [];
            state.mesData.forEach(item => {
              if (item.isSelect) {
                arr.push(item.key);
              }
            })
            arr.forEach(i => {
                state.mesData.forEach((item, index) => {
                if (item.key === i) {
                    state.mesData.splice(index, 1);
                }
              })
            })
        },
    },

    actions: {
        async fetchMessages({ commit },payload) {
            const { code, extend: { MessageInfo } } = await api.Messages(payload);
            if(code === 200){
                commit('saveMessage', { 
                    list: MessageInfo.list, 
                    mesPagination: { 
                        total: MessageInfo.total, 
                        pageNum: MessageInfo.pageNum, 
                        pageSize: MessageInfo.pageSize 
                    } 
                }
                );
            }
        },
        async fetchDeleteMessage({ commit },payload) {
            const { code } = await api.deletemoreMessage(payload);
            if(code === 200){
                commit('deleteChange');
                Message({
                    type: 'success',
                    message: '删除成功。',
                    showClose: true
                });
            }else{
                Message({
                    type: 'error',
                    message: '删除失败,请重试',
                    showClose: true
                });
            }
        },
        async fetchSetRead({ commit },payload) {
            const { code } = await api.setMoreMessageRead(payload);
            if(code === 200){
                commit('readChange',payload);
                commit('user/saveMesCount',-payload.length);
            }
        },
        async fetchEvaluations({ commit },payload) {
            const { code, extend: { pageInfo } } = await api.Evaluations(payload);
            if(code === 200){
                commit('saveEvaluate',{ 
                    list: pageInfo.list, 
                    evaPagination: { total: pageInfo.total, pageNum: pageInfo.pageNum, pageSize: 9 } 
                });
                commit('reloadPersonPagination', true);
            }
        },
    }
}

export default message